package com.base.doublepoint.slidingwindows;

import java.util.Arrays;

public class CheckInclusion {
    public boolean checkInclusion(String s1, String s2) {
        /**
         * 给你两个字符串 s1 和 s2 ，写一个函数来判断 s2 是否包含 s1 的排列。
         *
         * 换句话说，s1 的排列之一是 s2 的 子串
         */

        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        int left=0,right = c1.length-1;
        while (right<c2.length){
            if(permutations(c1,c2,left,right)){
                return true;
            }
            left++;
            right++;
        }
        return false;

    }

    public boolean permutations(char[] s1, char[] s2, int start, int end) {

        Arrays.sort(s1);
        s2 = Arrays.copyOfRange(s2, start, end + 1);
        Arrays.sort(s2);
        return Arrays.equals(s1,s2);
    }

    public static void main(String[] args) {
        String  s1= "ab";
        String  s2 = "cdfba";
        CheckInclusion checkInclusion = new CheckInclusion();
        System.out.println(checkInclusion.checkInclusion(s1,s2));
    }
}
